| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175 |
1
32
32
32
2
2
2
2
2
4
4
4
16
16
16
19
19
19
19
19
5
19
19
5
19
19
8
6
8
13
8
19
19
51
51
39
39
19
12
12
12
6
2
2
2
2
2
4
4
16
16
16
32
32
32
32
32
34
34
32
105
105
84
84
32
10
10
10
| 'use strict';
angular.module('mgcrea.ngStrap.button', [])
.provider('$button', function() {
var defaults = this.defaults = {
activeClass:'active',
toggleEvent:'click'
};
this.$get = function() {
return {defaults: defaults};
};
})
.directive('bsCheckboxGroup', function() {
return {
restrict: 'A',
require: 'ngModel',
compile: function postLink(element, attr) {
element.attr('data-toggle', 'buttons');
element.removeAttr('ng-model');
var children = element[0].querySelectorAll('input[type="checkbox"]');
angular.forEach(children, function(child) {
var childEl = angular.element(child);
childEl.attr('bs-checkbox', '');
childEl.attr('ng-model', attr.ngModel + '.' + childEl.attr('value'));
});
}
};
})
.directive('bsCheckbox', function($button, $$rAF) {
var defaults = $button.defaults;
var constantValueRegExp = /^(true|false|\d+)$/;
return {
restrict: 'A',
require: 'ngModel',
link: function postLink(scope, element, attr, controller) {
var options = defaults;
// Support label > input[type="checkbox"]
var isInput = element[0].nodeName === 'INPUT';
var activeElement = isInput ? element.parent() : element;
var trueValue = angular.isDefined(attr.trueValue) ? attr.trueValue : true;
if(constantValueRegExp.test(attr.trueValue)) {
trueValue = scope.$eval(attr.trueValue);
}
var falseValue = angular.isDefined(attr.falseValue) ? attr.falseValue : false;
if(constantValueRegExp.test(attr.falseValue)) {
falseValue = scope.$eval(attr.falseValue);
}
// Parse exotic values
var hasExoticValues = typeof trueValue !== 'boolean' || typeof falseValue !== 'boolean';
if(hasExoticValues) {
controller.$parsers.push(function(viewValue) {
// console.warn('$parser', element.attr('ng-model'), 'viewValue', viewValue);
return viewValue ? trueValue : falseValue;
});
// modelValue -> $formatters -> viewValue
controller.$formatters.push(function(modelValue) {
// console.warn('$formatter("%s"): modelValue=%o (%o)', element.attr('ng-model'), modelValue, typeof modelValue);
return angular.equals(modelValue, trueValue);
});
// Fix rendering for exotic values
scope.$watch(attr.ngModel, function(newValue, oldValue) {
controller.$render();
});
}
// model -> view
controller.$render = function () {
// console.warn('$render', element.attr('ng-model'), 'controller.$modelValue', typeof controller.$modelValue, controller.$modelValue, 'controller.$viewValue', typeof controller.$viewValue, controller.$viewValue);
var isActive = angular.equals(controller.$modelValue, trueValue);
$$rAF(function() {
if(isInput) element[0].checked = isActive;
activeElement.toggleClass(options.activeClass, isActive);
});
};
// view -> model
element.bind(options.toggleEvent, function() {
scope.$apply(function () {
// console.warn('!click', element.attr('ng-model'), 'controller.$viewValue', typeof controller.$viewValue, controller.$viewValue, 'controller.$modelValue', typeof controller.$modelValue, controller.$modelValue);
Iif(!isInput) {
controller.$setViewValue(!activeElement.hasClass('active'));
}
if(!hasExoticValues) {
controller.$render();
}
});
});
}
};
})
.directive('bsRadioGroup', function() {
return {
restrict: 'A',
require: 'ngModel',
compile: function postLink(element, attr) {
element.attr('data-toggle', 'buttons');
element.removeAttr('ng-model');
var children = element[0].querySelectorAll('input[type="radio"]');
angular.forEach(children, function(child) {
angular.element(child).attr('bs-radio', '');
angular.element(child).attr('ng-model', attr.ngModel);
});
}
};
})
.directive('bsRadio', function($button, $$rAF) {
var defaults = $button.defaults;
var constantValueRegExp = /^(true|false|\d+)$/;
return {
restrict: 'A',
require: 'ngModel',
link: function postLink(scope, element, attr, controller) {
var options = defaults;
// Support `label > input[type="radio"]` markup
var isInput = element[0].nodeName === 'INPUT';
var activeElement = isInput ? element.parent() : element;
var value;
attr.$observe('value', function(v) {
value = constantValueRegExp.test(v) ? scope.$eval(v) : v;
controller.$render();
});
// model -> view
controller.$render = function () {
// console.warn('$render', element.attr('value'), 'controller.$modelValue', typeof controller.$modelValue, controller.$modelValue, 'controller.$viewValue', typeof controller.$viewValue, controller.$viewValue);
var isActive = angular.equals(controller.$modelValue, value);
$$rAF(function() {
if(isInput) element[0].checked = isActive;
activeElement.toggleClass(options.activeClass, isActive);
});
};
// view -> model
element.bind(options.toggleEvent, function() {
scope.$apply(function () {
// console.warn('!click', element.attr('value'), 'controller.$viewValue', typeof controller.$viewValue, controller.$viewValue, 'controller.$modelValue', typeof controller.$modelValue, controller.$modelValue);
controller.$setViewValue(value);
controller.$render();
});
});
}
};
});
|